package pkg

import (
	"context"
	"flag"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

// 定义全局变量
var (
	region     string // 存储区域
	bucketName string // 存储空间名称
	objectName string // 对象名称
)

// init函数用于初始化命令行参数
func init() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
	flag.StringVar(&objectName, "object", "", "The name of the source object.")
}

func UploadImg(filename string) bool {

	// 加载默认配置并设置凭证提供者和区域
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewStaticCredentialsProvider("LTAI5tNQVmuaPWtGG1pEGWtw", "HWyNhzKsgIFEUGgP2piAhNitxJ4lfK")).
		WithRegion("cn_shanghai")

	// 创建OSS客户端
	client := oss.NewClient(cfg)

	// 创建上传管理器
	u := client.NewUploader()

	// 定义本地文件路径，需要替换为您的实际本地文件路径和文件名称
	localFile := "./status/img/" + filename

	// 执行上传文件的操作
	result, err := u.UploadFile(context.TODO(),
		&oss.PutObjectRequest{
			Bucket: oss.Ptr("yzly"),
			Key:    oss.Ptr(filename)},
		localFile)
	if err != nil {
		log.Fatalf("failed to upload file %v", err)
	}
	if err != nil {
		return false
	}
	// 打印上传文件的结果
	log.Printf("upload file result:%#v\n", result)
	return true
}
